Skip to content

63. Unique Paths II#32

Open
tom4649 wants to merge 2 commits intomainfrom
63.Unique-Paths-II
Open

63. Unique Paths II#32
tom4649 wants to merge 2 commits intomainfrom
63.Unique-Paths-II

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 28, 2026

63/sol1.py Outdated
if not obstacleGrid or not obstacleGrid[0]:
return 0

m, n = len(obstacleGrid), len(obstacleGrid[0])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m, nrow, col との対応が分かりにくいので、num_rows, num_cols などと置くといいのではないでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

採用させていただきました

63/sol1.py Outdated
Comment on lines +8 to +10
unique_paths_per_row = [1 - o for o in obstacleGrid[0]]
for col in range(1, n):
unique_paths_per_row[col] *= unique_paths_per_row[col - 1]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obstacleGrid の要素が 0 あるいは 1 であることを利用した算術による初期化で、ちょっとトリッキーに感じました。例えば障害物の有無を文字列で表すようになったときなど、仕様変更にも弱いように見えます。私がいまパッと書け、と言われたら次のようにします。

Suggested change
unique_paths_per_row = [1 - o for o in obstacleGrid[0]]
for col in range(1, n):
unique_paths_per_row[col] *= unique_paths_per_row[col - 1]
EMPTY = 0
if obstacleGrid[0][0] != EMPTY:
return 0
unique_paths_per_row = [1] + [0] * (n - 1)
for col in range(1, n):
if obstacleGrid[0][col] != EMPTY:
break
unique_paths_per_row[col] = unique_paths_per_row[col - 1]

偉そうなことを言いながらあんまり上手く書けている気もしないのですが、「左隣 col-1 を引き継ぐ。ただし障害物があったらそこから先はゼロ」感が出るとよいのではないでしょうか。

かなり書き方の幅が広いように思うので、もうちょっと他の人の書き方を漁ってみても良さそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アドバイスを参考にして以下のように書き直しました。(ほぼ同じですが)

        EMPTY = 0

        if obstacleGrid[0][0] != EMPTY:
            return 0

        unique_paths_per_row = [1] + [0] * (num_cols - 1)

        col = 1
        while col < num_cols and obstacleGrid[0][col] == EMPTY:
            unique_paths_per_row[col] = 1
            col += 1

63/sol3.py Outdated
unique_paths_per_row[col] = 0
continue

ways = unique_paths_per_row[col]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathway は同じ意味で使っているので、英単語も揃えたほうが分かりやすそうです。

63/sol3.py Outdated
Comment on lines +22 to +25
if col + 1 < n_col and not obstacleGrid[row][col + 1]:
unique_paths_per_row[col + 1] += ways
if row + 1 < n_row and not obstacleGrid[row + 1][col]:
unique_paths_next_row[col] += ways
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配る相手セルが障害物で潰れているかどうかは、ここでは気にしなくて良さそうです。

Suggested change
if col + 1 < n_col and not obstacleGrid[row][col + 1]:
unique_paths_per_row[col + 1] += ways
if row + 1 < n_row and not obstacleGrid[row + 1][col]:
unique_paths_next_row[col] += ways
if col + 1 < n_col:
unique_paths_per_row[col + 1] += ways
if row + 1 < n_row:
unique_paths_next_row[col] += ways

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、配る側のセルになったときに0になるので処理が重複していましたね

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモリ使用量が増えるのをやむなしで、2次元配列(フルのテーブル)で書いたほうが、配るDPは分かりやすいと思いました。また、最終行は配らなくていいとか、障害物があったら0に潰して配れない(こっちは最終行も処理する必要がある)、など制御が比較的面倒で、配るよりも貰う方が書きやすそうに感じました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「配るよりも貰う方が書きやすそう」は同意します。
配るDPでも直前の二行を保持しておけば良いので、一次元にする必要はないのではないかとと個人的には思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配るDPでも直前の二行を保持しておけば良いので、一次元にする必要はない

そのとおりで、処理的には問題ないのですが、書きやすさ(バグの埋め込みやすさ)・読みやすさの観点からは、2次元配列の方が優れていそうだな、と感じました。unique_paths_per_rowunique_paths_next_row の入れ替え(更新)の必要がなかったり、最終行は入れ替えしないという条件が理解しやすかったりしそうです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants